package com.example.innospace.demos.web.controller;

import com.example.innospace.demos.pojo.Request.ProjectUploadRequest;
import com.example.innospace.demos.pojo.dto.MidtermExtensionRequestDTO;
import com.example.innospace.demos.pojo.dto.PhaseTypeUpdateDTO;
import com.example.innospace.demos.pojo.dto.ProjectPageQueryDTO;
import com.example.innospace.demos.pojo.dto.ProjectUpdateStudentsDTO;
import com.example.innospace.demos.pojo.entity.Project;
import com.example.innospace.demos.pojo.enums.MessageEnum;
import com.example.innospace.demos.pojo.enums.ResponseCodeEnum;
import com.example.innospace.demos.pojo.result.PageResult;
import com.example.innospace.demos.pojo.result.R;
import com.example.innospace.demos.pojo.vo.ProjectVO;
import com.example.innospace.demos.web.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 项目操作
 */
@RequestMapping("/project")
@RestController
public class ProjectController {

    @Autowired
    private ProjectService projectService;


    /**
     * 创建项目
     * 只添加了项目最基本的信息，以及文件
     * @param request 项目内容
     * @param files 项目文件
     * @return message
     */
    @PostMapping("/project/add" )
    public R uploadProject( @RequestPart("request")ProjectUploadRequest request,
                           @RequestPart("files") List<MultipartFile> files) throws IOException {
        System.out.println("项目接口");

            MessageEnum messageEnum=projectService.saveProject(request,files);
            if(messageEnum==MessageEnum.PROJECT_UPLOAD_SUCCESS)
            return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.PROJECT_UPLOAD_SUCCESS);
            else
                return R.error(ResponseCodeEnum.NOT_FOUND,messageEnum);


    }


    /**
     * 修改项目内容
     * @param project
     * @param files
     * @return
     */
    @PutMapping("/project/updateproject")
    public R updateProject(@RequestPart("project") Project project ,@RequestPart("files") List<MultipartFile> files)
    {
        projectService.updateProject(project,files);
        return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
    }


    /**
     * 查看项目详情
     * @param id 项目id
     * @return 项目详情
     */
    @GetMapping("/project/info")
    public R<ProjectVO> info(@RequestParam int id)
    {
        ProjectVO projectVO=new ProjectVO();
        projectVO=projectService.getInfoById(id);
        return R.success(ResponseCodeEnum.SUCCESS,projectVO,MessageEnum.OPERATION_SUCCESS);
    }



    /**
     * 变更成员
     * @param dto 封装的请求参数
     * @return 操作结果
     * 将新的成员名单传入
     */
    @PostMapping("/project/updateStudents")
    public R updateStudents(@RequestBody ProjectUpdateStudentsDTO dto) {
        List<String> studentIds = dto.getStudentIds();
        String studentId = dto.getStudentId();
        int id = dto.getId();

        // 变更成员
        boolean flag = projectService.updateStudents(studentIds, studentId, id);
        if (flag) {
            return R.success(ResponseCodeEnum.SUCCESS, MessageEnum.OPERATION_SUCCESS);
        } else {
            return R.error(ResponseCodeEnum.BAD_REQUEST, MessageEnum.OPERATION_FAILED);
        }
    }
    /**
     * 变更第一指导老师
     * @param teacherId 原指导老师
     * @param updaterId 更新的新的老师id
     * @param id 项目id
     * @return 返回message
     */
    @PostMapping("/project/updateTeacherfirst")
    public R updateTeacherFirst(@RequestParam("teacherId") String teacherId,
                                @RequestParam("updaterId") String updaterId,
                                @RequestParam("id") Integer id) {
        projectService.updateTeacherFirst(teacherId, updaterId, id);
        return R.success(ResponseCodeEnum.SUCCESS, MessageEnum.OPERATION_SUCCESS);
    }

    /**
     * 变更第二指导老师
     * @param teacherId 前指导老师工号
     * @param updaterId 新更新的指导老师工号
     * @param id 项目id
     * @return 返回结果
     */
    @PostMapping("/project/updateTeachersecond")
    public R updateTeacherSecond(@RequestParam("teacherId") String teacherId,
                                 @RequestParam("updaterId") String updaterId,
                                 @RequestParam("id") Integer id)
    {
        projectService.updateTeacherSecond(teacherId,updaterId,id);
        return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
    }

    /**
     * 修改项目级别
     * @param id 项目id
     * @param level 项目级别
     * @return 返回结果
     */
    @PutMapping("/project/updatelevel")
    public R updateLevel(@RequestParam Integer id, @RequestParam String level)
    {
        projectService.updateLevel(id,level);
        return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
    }
    /**
     * 修改项目编号
     * @param id 项目id
     * @param projectUUID 设计的项目编号
     * @return 返回结果
     */
    @PutMapping("/project/updateProjectUUID")
    public R updateProjectUUID(@RequestParam Integer id,@RequestParam String projectUUID)
    {
        projectService.updateProjectUUID(id,projectUUID);
        return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
    }



    /**
     * 中期和结题检查的分页查询
     * @param pageNum 分页数量
     * @param pageSize 分页大小
     * @param projectPageQueryDTO 存储结果
     * @return 返回查询结果
     */
    @PostMapping("/page")
    public R<PageResult> getadminByPage(@RequestParam(defaultValue = "1") int pageNum,
                                        @RequestParam(defaultValue = "10") int pageSize,@RequestBody ProjectPageQueryDTO projectPageQueryDTO)
    {
        PageResult pageResult=projectService.pageQuery(pageNum,pageSize,projectPageQueryDTO);
        return R.success(ResponseCodeEnum.SUCCESS,pageResult,MessageEnum.OPERATION_SUCCESS);
    }


    /**
     * 学校通过申请
     * @param projectId 项目id
     * @param isPassed 是否通过
     * @return 是否审核
     */
    @PutMapping("/projects/schoolApproval")
    public  R updateSchoolApprovalStatus(@RequestParam Integer projectId,@RequestParam boolean isPassed)
    {
        projectService.updateSchoolApprovalStatus(projectId,isPassed);
        return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
    }

    /**
     * 学院通过申请
     * @param projectId 项目id
     * @param isPassed 是否通过
     * @return 结果
     */
    @PutMapping("/projects/collegeApproval")
    public  R updateCollegeApprovalStatus(@RequestParam Integer projectId,@RequestParam  boolean isPassed)
    {
        projectService.updateCollegeApprovalStatus(projectId,isPassed);
        return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
    }


    /**
     * 删除项目
     * @param projectId
     * @return
     */
    @DeleteMapping("/project/delete")
    public R deleteProject(@RequestParam Integer projectId)
    {
        projectService.deleteByProjectId(projectId);
        return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.DELETE_SUCCESS);
    }

    /*@PostMapping("project/submit/{projectId}")
    public R submitMidFile()*/


    /**
     * 提出中期延期申请
     * @param dto 包含是否提交和项目id的DTO对象
     * @return
     */
    @PostMapping("/submitMidtermExtensionRequest")
    public R submitMidtermExtensionRequest(@RequestBody MidtermExtensionRequestDTO dto) {
        if(dto.isProjectExtended()==true)
        {
            projectService.submitMidtermExtensionRequest(dto.isProjectExtended(), dto.getProjectId());
            return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
        }
        else
        {
            projectService.submitMidtermExtensionRequest(dto.isProjectExtended(), dto.getProjectId());
            return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
        }

    }





    /**
     * 提交中期报告
     * @param projectId 项目id
     * @param file 中期报告文件
     * @return message
     * @throws IOException
     */
    @PostMapping("/project/mid/submit")
    public R submitMidtermApplication(@RequestParam Integer projectId,
                                      @RequestParam MultipartFile file) throws IOException {
        if (!projectService.submitMidtermApplication(projectId, file)) {
            return R.error(ResponseCodeEnum.BAD_REQUEST,MessageEnum.OPERATION_FAILED);
        } else {
            return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);
        }
    }


    /**
     * 通过当前状态申请，终止，进入下一阶段阶段
     * @param dto 包含阶段状态和项目id的DTO对象
     * @return
     */
    @PostMapping("/update/PhaseType")
    public R updatePhaseType(@RequestBody PhaseTypeUpdateDTO dto) {
        if(dto.getPhaseType()==null)
            return R.error(ResponseCodeEnum.BAD_REQUEST,MessageEnum.PARAMETER_INVALID);
        try {
            projectService.updatePhaseType(dto.getPhaseType(), dto.getProjectId());
            return R.success(ResponseCodeEnum.SUCCESS, MessageEnum.OPERATION_SUCCESS);
        } catch (Exception e) {
            return R.error(ResponseCodeEnum.BAD_REQUEST, MessageEnum.OPERATION_FAILED);
        }
    }


    /**
     * 中期申请终止，
     */


}
